package com.demo.richText.util;


import com.demo.richText.Enum.ExceptionEnum;
import com.demo.richText.Enum.MessageEnum;
import com.demo.richText.constant.Constants;
import com.demo.richText.exception.RichException;
import lombok.Data;

import java.io.Serializable;

/**
 * @Description 返回结果封装
 * @Author 小乌龟
 * @Date 2022/8/29 19:47
 */
@Data
public class ResultResponse<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer code;
    private String msg;
    private T data;

    public ResultResponse() {
    }

    private ResultResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ResultResponse<T> ok() {
        return new ResultResponse<>(Constants.OK_CODE, Constants.OK_MSG, null);
    }

    public static <T> ResultResponse<T> ok(T data) {
        return new ResultResponse<>(Constants.OK_CODE, Constants.OK_MSG, data);
    }

    public static <T> ResultResponse<T> ok(String msg, T data) {
        return new ResultResponse<>(Constants.OK_CODE, msg, data);
    }

    public static <T> ResultResponse<T> fail(String msg) {
        return new ResultResponse<>(Constants.FAIL_CODE, msg, null);
    }

    public static <T> ResultResponse<T> fail(MessageEnum messageEnum, T data) {
        return new ResultResponse<>(messageEnum.getCode(), messageEnum.getMessage(), data);
    }

    public static <T> ResultResponse<T> fail(int errorCode, String msg) {
        return new ResultResponse<>(errorCode, msg, null);
    }

    public static <T> ResultResponse<T> fail(int errorCode, String msg,T data) {
        return new ResultResponse<>(errorCode, msg, data);
    }

    /**
     *自定义异常
     * @param be
     * @return
     */
    public static <T> ResultResponse<T> customException(RichException be){
        ResultResponse<T> result = new ResultResponse<>();
        result.setCode(be.getErrorCode());
        result.setMsg(be.getErrorMsg());
        result.setData(null);
        return result;
    }




    /**
     * 全局异常
     * @param exceptionEnum
     * @return
     */
    public static <T> ResultResponse<T> otherException(ExceptionEnum exceptionEnum){
        ResultResponse<T> result = new ResultResponse<>();
        result.setCode(exceptionEnum.getCode());
        result.setMsg(exceptionEnum.getMessage());
        result.setData(null);
        return result;
    }

}

